package cn.kgc.controller;

import cn.kgc.pojo.GUser;
import cn.kgc.pojo.Pdm;
import cn.kgc.pojo.Salary;
import cn.kgc.service.GUserService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: X9527M
 * @date: 2022/2/25
 * @description:
 */
@Controller
@RequestMapping("/xm")
public class GController {

    @Resource
    private GUserService gs;

    @RequestMapping("guanli")
    public String kan() {
        return "yonghuguanli";
    }


    @RequestMapping("/show")
    @ResponseBody
    public String show(@RequestParam Integer currentPage,
                       @RequestParam Integer pageSize,
                       @RequestParam(required = false) String userName,
                       @RequestParam(required = false) String rname,
                       @RequestParam(required = false) String brName) {
        PageHelper.startPage(currentPage, pageSize);
        Page<GUser> GUser = gs.GUser(userName, rname, brName);
        PageInfo<GUser> pageInfo = new PageInfo<GUser>(GUser);
        return JSON.toJSONString(pageInfo);
    }

    /**
     * 删除方法
     *
     * @param id
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public String del(@RequestParam int id) {
        boolean del = gs.del(id);
        return JSON.toJSONString(del);
    }


    /**
     * 跳转明细页面
     *
     * @return
     */
    @RequestMapping("/yonghumingxi/{id}")
    public String yonghumingxi(@PathVariable Integer id, Model model) {
        List<GUser> pdms = gs.selId(id);
        model.addAttribute("pdms", pdms);
        return "yonghuyemian";
    }

    @RequestMapping("/yonghuxiugai/{id}")
    public String yonghu(@PathVariable Integer id, Model model) {
        List<GUser> pdms = gs.selId(id);
        model.addAttribute("pdms", pdms);
        return "yonghuxiugai";
    }


    @RequestMapping("/upd")
    @ResponseBody
    public Boolean upd(@RequestParam Integer id,
                       @RequestParam String userCode,
                       @RequestParam String userName,
                       @RequestParam String userPwd,
                       @RequestParam Integer rId) {
        GUser gUser = new GUser();
        gUser.setId(id);
        gUser.setUserCode(userCode);
        gUser.setUserName(userName);
        gUser.setUserPwd(userPwd);
        gUser.setRId(rId);
        boolean bo = gs.upd(gUser);
        return bo;
    }

    @RequestMapping("/yonghutianjia")
    public String yonghuadd() {
        return "UserAdd";
    }
    @RequestMapping("/add")
    @ResponseBody
    public Boolean add(@RequestParam String userCode,
                       @RequestParam String userName,
                       @RequestParam String userPwd,
                       @RequestParam Integer rId) {
        GUser gUser = new GUser();
        gUser.setUserCode(userCode);
        gUser.setUserName(userName);
        gUser.setUserPwd(userPwd);
        gUser.setRId(rId);
        boolean bo = gs.add(gUser);
        return bo;
    }
    //判断添加用户，用户名存在的唯一性
    @RequestMapping("/isUniqueUserCode")
    //转换json数据  @ResponseBody
    @ResponseBody
    public String isUniqueUserCode(String userCode){
        boolean bo = gs.findByUserCode(userCode);
        return JSON.toJSONString(bo);
    }
}
